﻿using System.Configuration;
using System.Diagnostics;
using System.IO;
using UpdaterComponents.Tasks;

namespace UpdaterComponents.Common
{
    public class CommonMethods
    {
        public static void CleanUpIfAnyUndeletedData()
        {
            string sUpdatesFile = ConfigurationManager.AppSettings["ReleasesXmlFile"];

            if (Directory.Exists("NewRelease"))
                Directory.Delete("NewRelease", true);


            if (Directory.Exists(BackUpCreator.BackUpFolderName))
                Directory.Delete(BackUpCreator.BackUpFolderName, true);


            if (File.Exists(sUpdatesFile))
                File.Delete(sUpdatesFile);
            
        }

      
        public static void KillCurrentProcessAndRestartApp()
        {

            string sAppName = ConfigurationManager.AppSettings["ApplicationName"];

            Process ftpClientProcess = new Process();
            ftpClientProcess.StartInfo.FileName = sAppName + ".exe";
            ftpClientProcess.Start();

            Process currProcess = Process.GetCurrentProcess();
            currProcess.Kill();

        }


        public static bool CopyData(string sSource, string sDest)
        {

            string sDrive = Path.GetPathRoot(sSource);
            sDrive = sDrive.Replace("\\", " ");
            if (!Directory.Exists(sDest))
            {
                Directory.CreateDirectory(sDest);
            }

            Process process = new Process();
            process.StartInfo.FileName = "BackUp.bat";
            process.StartInfo.Arguments = sSource + " " + sDest + " " + sDrive;

            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;

            process.Start();
            process.WaitForExit();

            return true;
        }
              
    }
}
